package com.GLM.Viewer.Circle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.GLM.Constants.Constant;
import com.GLM.Entity.UserInfoDetailEntity;
import com.GLM.Tools.DB.DAO.UserInfoDAO;
import com.GLM.Tools.Utils.Image.AsynImageLoader;
import com.GLM.Tools.Utils.JSON.JSONResponse;
import com.GLM.Tools.Utils.JSON.JSONUtil;
import com.GLM.Tools.Utils.JSON.JSONResponse.Status;
import com.GLM.Viewer.R;
import com.GLM.Viewer.Users.UserInfoDetailActivity;
import com.google.gson.Gson;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class CircleDetailUsersActivty extends Activity{
	private final static int USERSLIST_LOADING = 111;
	private final static int USERSLIST_LONDED = 222;
	private ListView mUsersList;
	private int classId;
	private AsynImageLoader imageLoader = new AsynImageLoader();
	private SimpleAdapter mUsersAdapter;
	private List<Map<String, Object>> mUsersItems = new ArrayList<Map<String, Object>>();
	private String[] usersItemKeys = new String[] { "username", "userloc" };
	private int[] usersItemIds = new int[] { R.id.user_nickname, R.id.user_loc};
	private Map<Integer, String> usersPics = new HashMap<Integer, String>();
	private Map<Integer, Integer> usersMap = new HashMap<Integer, Integer>();
	
	private final Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			Thread thread;
			switch (msg.what) {
			case USERSLIST_LOADING:
//				mProgressBar.setVisibility(View.VISIBLE);
				thread = new Thread(new Runnable() {
					public void run() {
						initCircleUsersList();
						mUsersAdapter = new SimpleAdapter(CircleDetailUsersActivty.this, mUsersItems, R.layout.view_circle_user_listitem, usersItemKeys, usersItemIds);
						mHandler.sendEmptyMessage(USERSLIST_LONDED);
					}
				});
				thread.start();
				break;

			case USERSLIST_LONDED:
				mUsersList.setAdapter(mUsersAdapter);
//				mProgressBar.setVisibility(View.GONE);
				bindItemClick();
				
				break;
			}
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_circle_users);
		Intent intent = getIntent();
		classId = intent.getIntExtra("classId", -1);
		mUsersList = (ListView)findViewById(R.id.circle_users_listview);
		mHandler.sendEmptyMessage(USERSLIST_LOADING);
	}
	
	public void initCircleUsersList(){
		JSONObject jsonData = new JSONObject();
		JSONResponse jsonResponse = null;
		try {
			jsonData.put(Constant.PARAM_USERID, UserInfoDAO.getUserInfo(CircleDetailUsersActivty.this).getUserId());
			jsonData.put(Constant.PARAM_CLASSID, classId);
			jsonData.put("class", 1);
			jsonResponse = JSONUtil.postRequest(Constant.REQ_WAY_GETFRIENDSSMS, jsonData);

			if (jsonResponse.getStatus() != Status.ERR) {
				JSONObject data = jsonResponse.getData();
				int status = -1;
				status = data.getInt(Constant.RESP_STATUS);
				if (status == Constant.RESP_STATUS_SUCCESS) {
					JSONArray jsonArray = data.getJSONArray("usersData");
					
					initCircleUsersListItemFromJSONObject(jsonArray);
				}
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	private void initCircleUsersListItemFromJSONObject(JSONArray jsonArray) throws JSONException {
		Gson gson = new Gson();
		for (int i = 0; i < jsonArray.length(); i++) {
			UserInfoDetailEntity userEntity = gson.fromJson(jsonArray.getJSONObject(i).toString(), UserInfoDetailEntity.class);
			Map<String, Object> item = new HashMap<String, Object>();
			item.put("username", userEntity.getUd_nickname());
			item.put("userloc", userEntity.getLoc_name());
			Log.d("username>>userloc", userEntity.getUd_nickname()+">>>"+userEntity.getLoc_name());
			mUsersItems.add(item);
			usersPics.put(i, userEntity.getUser_avatar());
			usersMap.put(i, userEntity.getUser_id());
		}

	}
	
	public OnScrollListener scrollListener = new OnScrollListener() {
		
		public void onScrollStateChanged(AbsListView view, int scrollState) {
			// TODO Auto-generated method stub
			
		}
		
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
			// TODO Auto-generated method stub
			LoadPic();
		}
	};
	
	public void LoadPic(){
		int count = mUsersList.getChildCount();
		for(int i=0; i<count; i++){
			View view = mUsersList.getChildAt(i);
			ImageView userAvatar = (ImageView)view.findViewById(R.id.user_avatar);
			String avatarUrl = usersPics.get(i);
			if(avatarUrl != null && avatarUrl != ""){
				imageLoader.showImageAsyn(userAvatar, Constant.URL_USER_MIDDLE+avatarUrl, R.drawable.glm_white);
			}
		}
	}
	public void bindItemClick(){
		mUsersList.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(CircleDetailUsersActivty.this,UserInfoDetailActivity.class);
				intent.putExtra(Constant.INTENT_USER_ID, usersMap.get(position));
				startActivity(intent);
			}
		});
		mUsersList.setOnScrollListener(scrollListener);
	}
}
